﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ihongma.WebData;
using System.Web.Script.Serialization;
using ihongma.Model;
using Ivony.Fluent;
using Ivony.Html;
using Ivony.Html.Parser;
using System.Linq;
using System.Text;
namespace ihongma.test
{
    public partial class _Default : System.Web.UI.Page
    {
        JumonyParser parser = new JumonyParser();
        protected void Page_Load(object sender, EventArgs e)
        {
           
            
         //   ServiceReference1.article news= new ServiceReference1.article();
         //   ServiceReference1.addserverSoapClient s = new ServiceReference1.addserverSoapClient();
         //s.
            //Model.ajax ajax = new Model.ajax();
            //string html = HttpRequestHelper.GetPageSourceByUrl("http://www.open-open.com/ajax/ajax20110826171338.htm", "gb2312");
           // string str = HtmlHlep.showinfo("//html/head/title", "text", html);
            //Response.Write(str);
            //
            
           //insert(html, "12");
            ////ihongma.WebData.HtmlHlep c = new ihongma.WebData.HtmlHlep();
            //ihongma.WebData.javascript javadata = new WebData.javascript(html, "dictlangc_ec");
             //JavaScriptSerializer java = new JavaScriptSerializer();
            // ajax.name = javadata.gettex("//h4/div[1]").Replace("<img src='resource/icon_1.gif'>","");
            //ajax.img = javadata.getinfo("src","//div/div/img[1]");
            //ajax.Homepage = javadata.getinfo("href", "//div/a[1]");
            //ajax.Download = javadata.getinfo("href", "//div/a[2]");
            //ajax.Sample = javadata.getinfo("href", "//div/a[3]");
            //ajax.tag = javadata.getinfo("text", "//div/a[4]");
            //ajax.contet = javadata.getinfo("text", "//div/div/em");
            //Response.Write(java.Serialize(ajax));
           
            //List<Model.UrlAge> index = c.htmllist(html, "dictlangc_ec", "//dl[1]/dd");
            //List<Model.UrlAge> indexclass = c.htmlinfolist(html, "sidebar", "//dl[1]/dt");
           // Response.Write(java.Serialize(indexclass) + "@@@" + java.Serialize(index));
            //List<Model.UrlAge> indexclass = HtmlHlep.htmllist(html, "dictlangc_ec", "//div/p[1]");
            //Response.Write(java.Serialize(indexclass));
            //BLL.category bll = new BLL.category();
            //foreach (Model.UrlAge m in indexclass)
            //{
            //    if (m.name != "栏目导航")
            //    {
            //        Model.category model = new Model.category();
            //        model.channel_id = 13;
            //        model.title = m.name;
            //        model.parent_id = 0;
            //        model.sort_id = 99;
            //        model.seo_title = "";
            //        model.seo_keywords = m.name;
            //        model.seo_description = m.name;
            //        model.link_url = "";
            //        model.img_url = "";
            //        model.content = "";
            //        int i = bll.Add(model);
            //        Response.Write("i:" + i + "</br>");
            //        if (i > 1)
            //        {
            //            foreach (Model.UrlAge mm in index)
            //            {
            //                Response.Write(mm.name.Substring(0, 1) + "</br>");
            //                if (m.name == mm.name.Substring(0, 1))
            //                {

            //                    Model.category models = new Model.category();
            //                    models.channel_id = 13;
            //                    models.title = mm.name;
            //                    models.parent_id = i;
            //                    models.sort_id = 99;
            //                    models.seo_title = mm.name;
            //                    models.seo_keywords = mm.name;
            //                    models.seo_description = mm.name;
            //                    models.link_url = mm.Url;
            //                    models.img_url = "";
            //                    models.content = "";
            //                    bll.Add(models);

            //                }
            //            }
            //        }
            //    }
            //}
        }
        public class link
        {
            public  string url{get;set;}
            public  string img{get;set;}
        }
        public void insert(int id)
        {
            ServiceReference1.addserverSoapClient news = new ServiceReference1.addserverSoapClient();

            for (int i = int.Parse(newsS.Text); i < int.Parse(newsE.Text); i++)
            {
                string url = newsurl.Text;
                if (i!= 0)
                {
                    url = url + "/page/" + i.ToString();
                }
                string html = HttpRequestHelper.GetPageSourceByUrl(url, "utf-8");
                var dataDocument = parser.Parse(html);
                var link = dataDocument.Find(".entry-header h2 a");
                var img = dataDocument.Find(".post-thumb img");
                List<link> list=new List<link>();
                for (int j= 0; j< link.LongCount(); j++)
                {
                    link k = new link();
                    k.img = img.ElementAt(j).Attribute("src").Value();
                    k.url = link.ElementAt(j).Attribute("href").Value();
                    list.Add(k);
                }
                foreach (var item in list)
                {
                    string showhtml = HttpRequestHelper.GetPageSourceByUrl(item.url, "utf-8");
                    dataDocument = parser.Parse(showhtml);
                    
                  
                    ServiceReference1.article newsmodle = new ServiceReference1.article();
                    var meta = dataDocument.Find("meta");
                    var title = dataDocument.Find(".entry-name a").Single().InnerText();
                    newsmodle.seo_title = title;
                    newsmodle.title = title;
                    newsmodle.seo_keywords = meta.ElementAt(1).Attribute("content").Value();
                    newsmodle.seo_description = meta.ElementAt(2).Attribute("content").Value();
                    var body = dataDocument.Find(".entry-content");
                    newsmodle.img_url = item.img;
                    newsmodle.category_id =int.Parse(classs.Text);
                    newsmodle.channel_id = 1;
                    newsmodle.author = "管理员";
                    newsmodle.sort_id = 99;
                    newsmodle.form = "本站";
                    newsmodle.link_url = "";
                    newsmodle.add_time = DateTime.Now;
                    newsmodle.zhaiyao = meta.ElementAt(2).Attribute("content").Value();
                    StringBuilder templateBuilder = new StringBuilder();
                    var counet = body.Find("p");
                    foreach (var it in counet)
                        templateBuilder.Append("<p>" + it.InnerHtml() + "</p>");
                    newsmodle.content = templateBuilder.ToString();
                    news.addnews(newsmodle, "s123456");

                        
                }
            }
           
            
            //string html = HttpRequestHelper.GetPageSourceByUrl("http://www.open-open.com/ajax/", "utf-8");
            //var dataDocument = parser.Parse(html);
            //var name = dataDocument.Find(".entry-header h2 a");
            //foreach (var item in name)
            //    Response.Write("<p>" + item.Attribute("href").Value() + "</p>");
        }
        public void insert(string html,string classid)
        {
            ihongma.WebData.javascript javadata = new WebData.javascript(html, "dictlangc_ec");
            JavaScriptSerializer java = new JavaScriptSerializer();
            Model.ajax ajax = new Model.ajax();
             ajax.name = javadata.gettex("//h4/div[1]").Replace("<img src='resource/icon_1.gif'>","").Replace("&nbsp;","");
             ajax.img = javadata.getinfo("src", "//div/div/img[1]");
             ajax.Homepage = javadata.getinfo("href", "//div/a[1]");
             ajax.Download = javadata.getinfo("href", "//div/a[2]");
             ajax.Sample = javadata.getinfo("href", "//div/a[3]");
             ajax.tag = javadata.gettag("text");
             ajax.contet = javadata.getinfo("text", "//div/div/em");
             ajax.addtime = DateTime.Now;
             ajax.category_id = int.Parse(classid);
             ajax.channel_id = 13;
             ajax.is_top = 1;
             BLL.ajax bll = new BLL.ajax();
             bll.Add(ajax);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
           // ListItem item=;
            // for(int i=0; i<DropDownList2.Items.Count;i++)
            //{
            //   string item=DropDownList2.Items[i].Text;
            //   string url=string.Empty;
            //   bool d = true;
            //   int j=1;
            //   while (d)
            //   {
            //       if (j== 1)
            //       {
            //           url = item;
            //       }
            //       else
            //       {
            //           url = (j).ToString() + "_" + item.ToString();
            //       }
            //       string html = HttpRequestHelper.GetPageSourceByUrl("http://www.open-open.com/ajax/" + url, "gb2312");
            //       if (html != null && html != "远程服务器返回错误: (404) 未找到。")
            //       {
            //           string str = HtmlHlep.showinfo("//html/head/title", "text", html);

            //           if (str == "Error 404: doughnut not found!")
            //           {
            //               d = false;
            //               break;
            //           }
            //           else
            //           {
            //               List<Model.UrlAge> indexclass = HtmlHlep.htmllist(html, "dictlangc_ec", "//div/p[1]");
            //               foreach (Model.UrlAge age in indexclass)
            //               {
            //                   html = HttpRequestHelper.GetPageSourceByUrl("http://www.open-open.com/ajax/" + age.Url, "gb2312");
            //                   if (html != null)
            //                       insert(html, DropDownList2.Items[i].Value);
            //               }
            //           }
            //       }
            //       else
            //       {
            //           d = false;
            //       }
            //       j = j + 1;
            //   }
            //}
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            insert(341);
        }
    }
}